// File generated from our OpenAPI spec by Stainless.

package com.metest.api.services.blocking.rest.actuator

import com.metest.api.core.ClientOptions
import com.metest.api.core.RequestOptions
import com.metest.api.core.checkRequired
import com.metest.api.core.handlers.emptyHandler
import com.metest.api.core.handlers.errorBodyHandler
import com.metest.api.core.handlers.errorHandler
import com.metest.api.core.handlers.jsonHandler
import com.metest.api.core.http.HttpMethod
import com.metest.api.core.http.HttpRequest
import com.metest.api.core.http.HttpResponse
import com.metest.api.core.http.HttpResponse.Handler
import com.metest.api.core.http.HttpResponseFor
import com.metest.api.core.http.json
import com.metest.api.core.http.parseable
import com.metest.api.core.prepare
import com.metest.api.models.rest.actuator.caches.CachClearParams
import com.metest.api.models.rest.actuator.caches.CachDeleteByNameParams
import com.metest.api.models.rest.actuator.caches.CachListParams
import com.metest.api.models.rest.actuator.caches.CachListResponse
import com.metest.api.models.rest.actuator.caches.CachRetrieveByNameParams
import com.metest.api.models.rest.actuator.caches.CachRetrieveByNameResponse
import java.util.function.Consumer
import kotlin.jvm.optionals.getOrNull

class CachServiceImpl internal constructor(private val clientOptions: ClientOptions) : CachService {

    private val withRawResponse: CachService.WithRawResponse by lazy {
        WithRawResponseImpl(clientOptions)
    }

    override fun withRawResponse(): CachService.WithRawResponse = withRawResponse

    override fun withOptions(modifier: Consumer<ClientOptions.Builder>): CachService =
        CachServiceImpl(clientOptions.toBuilder().apply(modifier::accept).build())

    override fun list(params: CachListParams, requestOptions: RequestOptions): CachListResponse =
        // get /rest/actuator/caches
        withRawResponse().list(params, requestOptions).parse()

    override fun clear(params: CachClearParams, requestOptions: RequestOptions) {
        // delete /rest/actuator/caches
        withRawResponse().clear(params, requestOptions)
    }

    override fun deleteByName(params: CachDeleteByNameParams, requestOptions: RequestOptions) {
        // delete /rest/actuator/caches/{cache}
        withRawResponse().deleteByName(params, requestOptions)
    }

    override fun retrieveByName(
        params: CachRetrieveByNameParams,
        requestOptions: RequestOptions,
    ): CachRetrieveByNameResponse =
        // get /rest/actuator/caches/{cache}
        withRawResponse().retrieveByName(params, requestOptions).parse()

    class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
        CachService.WithRawResponse {

        private val errorHandler: Handler<HttpResponse> =
            errorHandler(errorBodyHandler(clientOptions.jsonMapper))

        override fun withOptions(
            modifier: Consumer<ClientOptions.Builder>
        ): CachService.WithRawResponse =
            CachServiceImpl.WithRawResponseImpl(
                clientOptions.toBuilder().apply(modifier::accept).build()
            )

        private val listHandler: Handler<CachListResponse> =
            jsonHandler<CachListResponse>(clientOptions.jsonMapper)

        override fun list(
            params: CachListParams,
            requestOptions: RequestOptions,
        ): HttpResponseFor<CachListResponse> {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "caches")
                    .build()
                    .prepare(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            val response = clientOptions.httpClient.execute(request, requestOptions)
            return errorHandler.handle(response).parseable {
                response
                    .use { listHandler.handle(it) }
                    .also {
                        if (requestOptions.responseValidation!!) {
                            it.validate()
                        }
                    }
            }
        }

        private val clearHandler: Handler<Void?> = emptyHandler()

        override fun clear(params: CachClearParams, requestOptions: RequestOptions): HttpResponse {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.DELETE)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "caches")
                    .apply { params._body().ifPresent { body(json(clientOptions.jsonMapper, it)) } }
                    .build()
                    .prepare(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            val response = clientOptions.httpClient.execute(request, requestOptions)
            return errorHandler.handle(response).parseable {
                response.use { clearHandler.handle(it) }
            }
        }

        private val deleteByNameHandler: Handler<Void?> = emptyHandler()

        override fun deleteByName(
            params: CachDeleteByNameParams,
            requestOptions: RequestOptions,
        ): HttpResponse {
            // We check here instead of in the params builder because this can be specified
            // positionally or in the params class.
            checkRequired("cache", params.cache().getOrNull())
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.DELETE)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "caches", params._pathParam(0))
                    .apply { params._body().ifPresent { body(json(clientOptions.jsonMapper, it)) } }
                    .build()
                    .prepare(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            val response = clientOptions.httpClient.execute(request, requestOptions)
            return errorHandler.handle(response).parseable {
                response.use { deleteByNameHandler.handle(it) }
            }
        }

        private val retrieveByNameHandler: Handler<CachRetrieveByNameResponse> =
            jsonHandler<CachRetrieveByNameResponse>(clientOptions.jsonMapper)

        override fun retrieveByName(
            params: CachRetrieveByNameParams,
            requestOptions: RequestOptions,
        ): HttpResponseFor<CachRetrieveByNameResponse> {
            // We check here instead of in the params builder because this can be specified
            // positionally or in the params class.
            checkRequired("cache", params.cache().getOrNull())
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "caches", params._pathParam(0))
                    .build()
                    .prepare(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            val response = clientOptions.httpClient.execute(request, requestOptions)
            return errorHandler.handle(response).parseable {
                response
                    .use { retrieveByNameHandler.handle(it) }
                    .also {
                        if (requestOptions.responseValidation!!) {
                            it.validate()
                        }
                    }
            }
        }
    }
}
